import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import QtQml 2.2

Rectangle {
    border.width: 1
    height: 20
    property var maxDate: 30
    property bool showCalender: false
    property var year: Qt.formatDateTime(new Date(), "yyyy")
    property var month: Qt.formatDateTime(new Date(), "MM")
    property var date: Qt.formatDateTime(new Date(), "dd")

    RowLayout {
        anchors.fill: parent
        TextInput {
            id: yearI
            anchors.left: parent.left
            anchors.leftMargin: 4
            //                        anchors.top: parent.top
            //                        anchors.bottom: parent.bottom
            Layout.preferredWidth: 28
            Layout.alignment: Qt.AlignVCenter
            selectByMouse: true
            text: year
            validator: RegExpValidator {
                regExp: /\d{1,4}/
            }
            onEditingFinished: {

                var nYear = parseInt(yearI.text)
                if (nYear < 30)
                    year = 2000 + nYear
                else if (nYear >= 30 && nYear < 100)
                    year = 1900 + nYear
                else if (nYear >= 100 && nYear < 1000)
                    year = Qt.formatDateTime(new Date(), "yyyy")
                else
                    year = nYear

                console.log("year onEditingFinished " + year)
                freshMaxDate()
            }
            onActiveFocusChanged: {
                if (activeFocus)
                    selectAll()
            }

            Keys.onPressed: {
                if (event.key == Qt.Key_Down)
                    year--
                else if (event.key == Qt.Key_Up)
                    year++
                else if (event.key == Qt.Key_Left) {
                    dateI.selectAll()
                    dateI.focus = true
                } else if (event.key == Qt.Key_Right) {
                    monthI.selectAll()
                    monthI.focus = true
                }
            }
        }
        Text {
            text: "/"
            Layout.alignment: Qt.AlignVCenter
        }
        TextInput {
            id: monthI
            //            anchors.top: parent.top
            //            anchors.bottom: parent.bottom
            Layout.preferredWidth: 12
            selectByMouse: true
            //            mouseSelectionMode: TextInput.SelectWords
            text: month
            validator: IntValidator {
                bottom: 1
                top: 12
            }

            onEditingFinished: {
                month = parseInt(monthI.text)
                console.log("month onEditingFinished " + month)
                freshMaxDate()
            }
            onActiveFocusChanged: {
                if (activeFocus)
                    selectAll()
            }
            Keys.onPressed: {
                if (event.key == Qt.Key_Down)
                    month--
                else if (event.key == Qt.Key_Up)
                    month++
                else if (event.key == Qt.Key_Left) {
                    yearI.selectAll()
                    yearI.focus = true
                } else if (event.key == Qt.Key_Right) {
                    dateI.selectAll()
                    dateI.focus = true
                }

                if (month > 12)
                    month = 1
                else if (month < 1)
                    month = 12
            }
        }
        Text {
            text: "/"
            Layout.alignment: Qt.AlignVCenter
        }
        TextInput {
            id: dateI
            Layout.fillWidth: true
            //            anchors.top: parent.top
            //            anchors.bottom: parent.bottom
            Layout.alignment: Qt.AlignVCenter
            selectByMouse: true
            text: date
            validator: IntValidator {
                bottom: 1
                top: maxDate
            }

            onEditingFinished: {
                console.log("date onEditingFinished")
            }
            onActiveFocusChanged: {
                if (activeFocus)
                    selectAll()
            }

            Keys.onPressed: {
                if (event.key == Qt.Key_Down)
                    date--
                else if (event.key == Qt.Key_Up)
                    date++
                else if (event.key == Qt.Key_Left) {
                    monthI.selectAll()
                    monthI.focus = true
                } else if (event.key == Qt.Key_Right) {
                    yearI.selectAll()
                    yearI.focus = true
                }

                if (date > maxDate)
                    date = 1
                else if (date < 1)
                    date = maxDate
            }
        }
        Button {
            //            text: "V"
            Layout.preferredWidth: 20
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.right: parent.right
            iconSource: "qrc:/Res/image/arrow_down.png"
            focus: true

            onClicked: {
                console.log("onClicked " + showCalender)
                if (!showCalender)
                    calendar.focus = true
                showCalender = !showCalender
            }
        }
    }
    Loader {
        id: calendar
        anchors.left: parent.left
        anchors.top: parent.bottom
        sourceComponent: showCalender ? Qt.createComponent(
                                            "PopCalendar.qml") : undefined
        onLoaded: item.curDate = new Date(year, month - 1, date)

        onActiveFocusChanged: {
            console.log("onActiveFocusChanged calendar " + activeFocus)
            if (!activeFocus)
                showCalender = false
        }

        onFocusChanged: {
            console.log("onFocusChanged calendar " + activeFocus)
            if (!activeFocus)
                showCalender = false
        }
    }

    Connections {
        target: calendar.item
        onSelectDate: {
            console.log("click calendar " + date1)
            showCalender = false
            year = date1.getFullYear()
            month = date1.getMonth() + 1
            date = date1.getDate()
        }
    }
    function freshMaxDate() {
        var nMonth = parseInt(month)
        var nYear = parseInt(year)
        if (2 === nMonth) {
            if (isLeapYear(nYear))
                maxDate = 29
            else
                maxDate = 28
        } else {
            if ((nMonth < 8 && 1 === nMonth % 2) || (nMonth >= 8
                                                     && 0 === nMonth % 2))
                maxDate = 31
            if ((nMonth < 8 && 0 == nMonth % 2) || (nMonth >= 8
                                                    && 1 === nMonth % 2))
                maxDate = 30
        }

        console.log(nMonth + " nMonth%2:" + nMonth % 2)
        console.log("freshMaxDate:" + maxDate)
    }

    function isLeapYear(y) {
        return ((0 == y % 4 && 0 != y % 100) || (0 == y % 400))
    }

    function getDateStr() {
        var str = String(year)
        if (String(month).length > 1)
            str += String(month)
        else
            str += "0" + String(month)

        if (String(date).length > 1)
            str += String(date)
        else
            str += "0" + String(date)

        return str
    }
}
